package com.zhanghp.class036;

import com.zhanghp.common.TreeNode;

/**
 * 求二叉树的最大、最小深度
 *
 * @author zhanghp
 * @since 2024/4/15 17:42
 */
public class Code04_DepthOfBinaryTree {

    // 测试链接 : https://leetcode.cn/problems/maximum-depth-of-binary-tree/
    public static int maxDepth(TreeNode root) {
        return root == null ? 0 : Math.max(maxDepth(root.left), maxDepth(root.right)) + 1;
    }

    // 测试链接 : https://leetcode.cn/problems/minimum-depth-of-binary-tree/
    public int minDepth(TreeNode root) {
        // 终止递归条件1
        if (root == null) {
            return 0;
        }
        // 终止递归条件2
        if (root.left == null && root.right == null) {
            return 1;
        }
        int ldepth = Integer.MAX_VALUE;
        int rdepth = Integer.MAX_VALUE;
        if (root.left != null) {
            ldepth = minDepth(root.left);
        }
        if (root.right != null) {
            rdepth = minDepth(root.right);
        }
        return Math.min(ldepth, rdepth) + 1;
    }
}
